﻿using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.Collections.Generic;
using System.Reflection;

// Config/log4net.config：哪个项目需要引用，这个配置文件就要在那个的项目里面。参考 Manage 项目配置文件
[assembly: XmlConfigurator(ConfigFile = @"Config/log4net.config", Watch = true)] // 读取指定路径的配置文件
namespace HCMS.Utils
{
    /// <summary>
    /// Log4Net 日志工具类
    /// </summary>
    public static class Log4NetUtil
    {
        private static readonly ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
        private static readonly ILog log = LogManager.GetLogger(repository.Name, "log4net");

        #region Info 日志
        /// <summary>
        /// Info 日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        public static void Info(string msg)
        {
            var logs = new List<string>
            {
                $"时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                $"消息：{msg}"
            };
            var msgResult = string.Join("\r\n", logs);

            log.Info(msgResult);
        }
        #endregion

        #region Debug 日志
        /// <summary>
        /// Debug 日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        public static void Debug(string msg)
        {
            var logs = new List<string>
            {
                $"时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                $"消息：{msg}"
            };
            var msgResult = string.Join("\r\n", logs);

            log.Debug(msgResult);
        }
        #endregion

        #region Error 日志
        /// <summary>
        /// Error 日志
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="innerException">异常</param>
        public static void Error(string msg, Exception? innerException = null)
        {
            var logs = new List<string>
            {
                $"时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                $"消息：{msg}"
            };
            var msgResult = string.Join("\r\n", logs);

            if (innerException != null)
            {
                Exception? ex = innerException;
                while (ex != null && ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }

                log.Error(msgResult, ex);
            }
            else
            {
                log.Error(msgResult);
            }
        }

        /// <summary>
        /// Error 日志
        /// </summary>
        /// <remarks>
        /// 返回 异常 Id
        /// </remarks>
        /// <param name="msg">日志内容</param>
        /// <param name="innerException">异常</param>
        /// <param name="errorId">返回 异常 Id</param>
        public static void Error(string msg, Exception innerException, out string errorId)
        {
            errorId = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), new Random().Next(1000, 9999));
            var logs = new List<string>
            {
                $"异常Id：{errorId}",
                $"时间：{DateTime.Now:yyyy-MM-dd HH:mm:ss}",
                $"消息：{msg}"
            };

            var msgResult = string.Join("\r\n", logs);

            if (innerException != null)
            {
                Exception? ex = innerException;
                while (ex != null && ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }

                log.Error(msgResult, ex);
            }
            else
            {
                log.Error(msgResult);
            }
        }
        #endregion
    }
}
